﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    public static partial class Extention
    {
        /// <summary>
        /// 尝试将键和值添加到字典中：如果不存在，才添加；存在，不添加也不抛导常
        /// </summary>
        public static Dictionary<TKey, TValue> TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
        {
            if (dict.ContainsKey(key) == false) dict.Add(key, value);
            return dict;
        }
        /// <summary>
        /// 将键和值添加或替换到字典中：如果不存在，则添加；存在，则替换
        /// </summary>
        public static Dictionary<TKey, TValue> AddOrReplace<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
        {
            dict[key] = value;
            return dict;
        }
        /// <summary>
        /// 获取与指定的键相关联的值，如果没有则返回输入的默认值
        /// </summary>
        public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default(TValue))
        {
            return dict.ContainsKey(key) ? dict[key] : defaultValue;
        }
        /// <summary>
        /// 向字典中批量添加键值对
        /// </summary>
        /// <param name="replaceExisted">如果已存在，是否替换</param>
        public static Dictionary<TKey, TValue> AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dict, IEnumerable<KeyValuePair<TKey, TValue>> values, bool replaceExisted)
        {
            foreach (var item in values)
            {
                if (dict.ContainsKey(item.Key) == false || replaceExisted)
                    dict[item.Key] = item.Value;
            }
            return dict;
        }
		#region 将URL中的参数转换成字典Dictionary<string, string>
        /// <summary>
        /// 将URL中的参数转换成字典Dictionary<string, string>
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Dictionary<string, string> UrlToDictionary(this string s)
        {
            var dic = new Dictionary<string, string>();
            if (s.Contains("&") || s.Contains("="))
            {
                var args = HttpUtility.ParseQueryString(s);
                if (args == null || args.Count <= 0) { return dic; }
                foreach (var key in args.AllKeys)
                {
                    if (key == null) { continue; }
                    if (dic.ContainsKey(key))
                    {
                        dic[key] = args[key];
                    }
                    else
                    {
                        dic.Add(key, args[key]);
                    }
                }
            }
            return dic;
        }
        /// <summary>
        /// 将Dictionary<string, string>转换成URL中的参数
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string DictionaryToUrl(this Dictionary<string, string> param)
        {
            StringBuilder builder = new StringBuilder();
            foreach (var entry in param)
            {
                if (string.IsNullOrWhiteSpace(Convert.ToString(entry.Key)) || null == entry.Value)
                {
                    continue;
                }
                builder.Append(Convert.ToString(entry.Key) + "=" + Convert.ToString(entry.Value) + "&");
            }
            return builder.ToString().Substring(0, builder.ToString().LastIndexOf("&")); ;
        }
        #endregion
    }
}
